<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="140px" @submit.native.prevent>
      <el-row>
        <el-col :span="8">
          <el-form-item label="装备型号" prop="equipModel">
            <el-input v-model="pram.equipModel" maxlength="100" placeholder="装备型号"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="任务名称" prop="taskName">
            <el-input v-model="pram.taskName" maxlength="100" placeholder="装备型号"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属单位" prop="taskName">
            <el-cascader v-model="pram.departmentId" :disabled="pram.departmentId ===0" :options="parentOptions"
                         :props="categoryProps" style="width:100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="问题时间" prop="questionTime">
            <el-date-picker v-model="pram.questionTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small"
                            type="date" placement="bottom-end" style="width: 100%;" @change="onchangeSTime"
                            placeholder="问题时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="问题名称" prop="questionName">
            <el-input v-model="pram.questionName" maxlength="100" placeholder="问题名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">

          <el-form-item label="需要机关协调解决">
            <el-radio-group v-model="pram.isUse">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="问题等级" prop="questionGrade">
            <el-input v-model="pram.questionGrade" maxlength="100" placeholder="问题等级"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="问题类型" prop="questionModel">
            <el-input v-model="pram.questionModel" maxlength="100" placeholder="问题类型"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="序号">
            <el-input-number v-model="pram.number" :min="0"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="解决进度" prop="resolutionProgress">
            <el-input v-model="pram.resolutionProgress" type="textarea" :rows="5"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="问题现象" prop="questionPhenomenon">
            <el-input v-model="pram.questionPhenomenon" type="textarea" :rows="5"/>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="问题原因" prop="questionCause">
            <el-input v-model="pram.questionCause" type="textarea" :rows="5"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="pram.remark" type="textarea" :rows="5"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')"
                   v-hasPermi="['admin:electronic:update','admin:electronic:save']"
        >{{ isCreate === 0 ? '确定' : '更新' }}
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import * as electronicApi from '@/api/electronic.js'
import { Debounce } from '@/utils/validate'
import { elcAdd } from '@/api/electronic.js'
import { addTestFailureApi, getTestsFailureApi } from '@/api/failure'

export default {
  // name: "edit"
  components: {},
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    id: {
      type: Number,
      required: true
    },
    allCategoryData: {
      type: Object
    },
    allTreeList: {
      type: Array
    }
  },
  data() {
    return {
      constants: this.$constants,
      pram: {
        equipModel: null,
        taskName: null,
        questionTime: null,
        questionName: null,
        questionPhenomenon: null,
        questionCause: null,
        questionGrade: null,
        questionModel: null,
        number: 0,
        resolutionProgress: null,
        isUse: 0,
        remark: null,
        departmentId: '',
        id: 0
      },
      rules: {
        // name: [{ required: true, message: '请填设备名称', trigger: ['blur', 'change'] }],
        // brand: [{ required: true, message: '请选择设备品牌', trigger: ['blur', 'change'] }],
        // device: [{ required: true, message: '请选择设备型号', trigger: ['blur', 'change'] }],
        // deviceType: [{ required: true, message: '请选择设备类型', trigger: ['blur', 'change'] }],
        // personId: [ { required: true, message: '请选择所属人', trigger: ['blur', 'change']} ],
        // serialization: [ { required: true, message: '请填机身串码', trigger: ['blur', 'change']} ],
        // mobile: [ { required: true, message: '请填手机号', trigger: ['blur', 'change']} ],
        // macAddress: [ { required: true, message: '请填Mac地址', trigger: ['blur', 'change']} ],
        // bluetoothAddress: [ { required: true, message: '请填蓝牙通讯地址', trigger: ['blur', 'change']} ],
      },
      parentOptions: [],
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
      categoryData: {
        brandList: [],
        deviceList: [],
        deviceTypeList: [],
        personList: []
      },
      deviceList: []
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    onchangeSTime(e) {
      this.pram.questionTime = e
    },

    initEditData() {
      this.parentOptions = [...this.allTreeList]
      if (this.isCreate === 1) {
        this.getTestsFailure()
      }

    },
    getTestsFailure() {
      getTestsFailureApi({ id: this.id }).then(data => {
        if (data) {
          this.pram = data
        }

      })
    },
    handlerSubmit: Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      addTestFailureApi(this.pram).then(data => {
        this.$message.success('创建设备成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      addTestFailureApi(this.pram).then(data => {
        this.$message.success('更新设备成功')
        this.$emit('hideEditDialog')
      })
    }
  }
}
</script>

<style scoped>

</style>
